﻿using Newtonsoft.Json;
using SelfService.Model.Interface;
using SelfService.Model.Models;
using SelfService.XinXing.Models;
using WalkingTec.Mvvm.Core;

namespace SelfService.XinXing
{
    public class XinXingPay : IPaySystem
    {
        private readonly AppWebService.AutoWebServiceSoapClient _appClient;


        public XinXingPay(AppWebService.AutoWebServiceSoapClient appClient)
        {
            _appClient = appClient;
        }

        /// <summary>
        /// 获取支付二维码
        /// </summary>
        /// <param name="pay"></param>
        /// <param name="client"></param>
        /// <param name="operator"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultDetail<IPay>> GetQrCode(SysPay pay, SysClient client, SysOperator @operator)
        {
            var request = new HisQrCodeRequest();
            request.Caption = "门诊充值";
            request.Descriptive = "门诊壁挂机挂号充值";
            request.CardNo = pay.Patient.CardNo;
            request.TotalAmount = pay.Amount;
            request.UserId = @operator.Code;
            //龙支付
            request.Zffs = "0E";
            request.Ip = client.Ip;
            var xml = request.ToXml();
            try
            {
                var response = await _appClient.GetValueAsync(xml);
                if (response.XmlTryParseToObject(out HisQrCodeResponse result))
                {
                    if (result.ResultCode == "0")
                    {
                        pay.OutOrderNo = result.OutTradeNo;
                        pay.PayUrl = result.TwoDimensionCode;
                        return new ResultDetail<IPay>()
                        {
                            Data = pay,
                            Request = xml,
                            Response = response
                        };
                    }
                    return new ResultDetail<IPay>()
                    {
                        Code = 1,
                        Message = result.ErrorMsg,
                        Request = xml,
                        Response = response
                    };
                }
                return new ResultDetail<IPay>()
                {
                    Code = 1,
                    Message = "获取二维码失败, 返回数据解析异常.",
                    Request = xml,
                    Response = response
                };
            }
            catch (Exception e)
            {
                return new ResultDetail<IPay>()
                {
                    Code = 1,
                    Message = $"获取二维码异常:{JsonConvert.SerializeObject(e)}",
                    Request = xml
                };
            }
        }

        /// <summary>
        /// 查询支付结果
        /// </summary>
        /// <param name="pay"></param>
        /// <param name="client"></param>
        /// <param name="operator"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultDetail<IPay>> GetPayResult(SysPay pay, SysClient client, SysOperator @operator)
        {
            var request = new HisPayResultRequest();
            request.Zffs = "0E";
            request.OutTradeNo = pay.OutOrderNo;
            request.CardNo = pay.Patient.CardNo;
            request.UserId = @operator.Code;
            request.Ip = client.Ip;
            var xml = request.ToXml();
            try
            {
                var response = await _appClient.GetValueAsync(xml);
                if (response.XmlTryParseToObject(out HisPayResultResponse result))
                {
                    if (result.ResultCode == "0")
                    {
                        if (result.TradeStatus == "TRADE_SUCCESS" || result.TradeStatus == "TRADE_FINISHED")
                        {
                            pay.PayStatus = PayStatusEnum.支付成功;
                            pay.PayTime = DateTime.Now;
                            return new ResultDetail<IPay>()
                            {
                                Data = pay,
                                Request = xml,
                                Response = response
                            };
                        }
                        else
                        {
                            return new ResultDetail<IPay>()
                            {
                                Code = 2,
                                Request = xml,
                                Response = response
                            };
                        }
                    }
                    else
                    {
                        return new ResultDetail<IPay>()
                        {
                            Code = 1,
                            Message = result.ErrorMsg,
                            Request = xml,
                            Response = response
                        };
                    }
                }
                return new ResultDetail<IPay>()
                {
                    Code = 1,
                    Message = "支付失败, 返回数据解析异常.",
                    Request = xml,
                    Response = response
                };
            }
            catch (Exception e)
            {
                return new ResultDetail<IPay>()
                {
                    Code = 1,
                    Message = $"支付异常:{JsonConvert.SerializeObject(e)}",
                    Request = xml
                };
            }
        }

        public Task<ResultDetail<IPay>> GetQrCode(SysPay pay, LoginUserInfo user)
        {
            throw new NotImplementedException();
        }

        public Task<ResultDetail<IPay>> MicroPay(SysPay pay, LoginUserInfo user)
        {
            throw new NotImplementedException();
        }

        public Task<ResultDetail<IPay>> GetPayResult(SysPay pay)
        {
            throw new NotImplementedException();
        }

        public Task<ResultDetail<IPay>> Refund(SysPay pay)
        {
            throw new NotImplementedException();
        }

        public Task<ResultDetail<IPay>> CheckRefund(SysPay pay)
        {
            throw new NotImplementedException();
        }
    }
}
